package com.fishbone.file.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import com.fishbone.file.config.OssProperties;

import java.io.File;
import java.io.InputStream;

/**
 * @ClassName FishBoneOssClient
 * @Author huyaxi
 * @DATE 2020/4/20 16:37
 */
public class AliOssClient {

    private OSSClient ossClient;

    public AliOssClient(OssProperties properties) {
        OSSClientBuilder builder = new OSSClientBuilder();
        this.ossClient = (OSSClient)builder.build(properties.getEndpoint(),properties.getAk(),properties.getSk());
    }


    /**
     * 获取OSS文件对象信息
     * @param bucketName
     * @param objectKey
     * @return
     */
    public OSSObject getOssObject(String bucketName,String objectKey) {
        return ossClient.getObject(bucketName, objectKey);
    }


    /**
     * 文件模式文件上传
     * @param bucket
     * @param objectKey
     * @param file
     * @return
     */
    public PutObjectResult upload(String bucket, String objectKey, File file){
        return ossClient.putObject(bucket, objectKey, file);
    }


    /**
     * 流模式文件上传
     * @param bucket
     * @param objectKey
     * @param inputStream
     * @return
     */
    public PutObjectResult upload(String bucket, String objectKey, InputStream inputStream) {
        return ossClient.putObject(bucket, objectKey, inputStream);
    }
}
